package com.mark.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @category 文件操作增、删、读、改的实现工具类
 * @version 1.0.0
 * @author Mark Liang
 *  
 */

public class FileCommon {
	
	/**
	 * @category 读取单个文件数据
	 * @param filePath 单个文件路径
	 * @return List<String>
	 * @throws IOException
	 */
	static public List<String> singleBrReader(String filePath) throws IOException{
		BufferedReader br = null;
		List <String> strList = new ArrayList <String>();
		FileInputStream file = new FileInputStream(filePath);
        InputStreamReader fileR = new InputStreamReader(file,"utf-8");
        br = new BufferedReader(fileR);	
		//BufferedReader tempStrm = br;
		String temp = "";
        while((temp = br.readLine()) != null){
        	//summbaLogger.info("temp :"+temp);
        	strList.add(temp);
        	//summbaLogger.info("temp : |"+temp); 
        }
        file.close();
        return strList;
	}
	
	/**
	 * @category 读取文件夹下多个文件数据
	 * @param filePath 文件存放目录
	 * @return
	 * @throws IOException
	 */
	static public List< List<String> > mutilBrReader(String filePath) throws IOException{
		//List<String> logList = new ArrayList<String>();
		List< List<String> > allLogList = new ArrayList< List <String>>();
		File file = new File(filePath);
		File[] array = file.listFiles(); 
		for (int i = 0; i < array.length; i++) {
			List<String> logList = singleBrReader( array[i].toString() );
			allLogList.add(logList);
		}
		return allLogList;
	}
	
	/**
	 * @param File
	 * @param content
	 * 
	 */
	public static void appendFile( String File ,String content ){
		try{
			//String File = "";
			OutputStreamWriter out;
			out = new OutputStreamWriter( new FileOutputStream(File,true),"utf-8");
			out.write( content );
			out.flush();
			out.close();
		}catch( Exception e){
			e.printStackTrace();
		}
	}
	
	public static boolean delete(String fileName){     
        File file = new File(fileName);     
        if(!file.exists()){     
            System.out.println("删除文件失败："+fileName+"文件不存在");     
            return false;     
        }else{     
            if(file.isFile()){     
                     
                return deleteFile(fileName);     
            }else{     
                return deleteDirectory(fileName);     
            }     
        }     
    } 
	
	public static boolean deleteFile(String fileName){     
        File file = new File(fileName);     
        if(file.isFile() && file.exists()){     
            file.delete();     
            System.out.println("删除单个文件"+fileName+"成功！");     
            return true;     
        }else{     
            System.out.println("删除单个文件"+fileName+"失败！");     
            return false;     
        }     
    }
	
    public static boolean deleteDirectory(String dir){     
        //如果dir不以文件分隔符结尾，自动添加文件分隔符     
        if(!dir.endsWith(File.separator)){     
            dir = dir+File.separator;     
        }     
        File dirFile = new File(dir);     
        //如果dir对应的文件不存在，或者不是一个目录，则退出     
        if(!dirFile.exists() || !dirFile.isDirectory()){     
            System.out.println("删除目录失败"+dir+"目录不存在！");     
            return false;     
        }     
        boolean flag = true;     
        //删除文件夹下的所有文件(包括子目录)     
        File[] files = dirFile.listFiles();     
        for(int i=0;i<files.length;i++){     
            //删除子文件     
            if(files[i].isFile()){     
                flag = deleteFile(files[i].getAbsolutePath());     
                if(!flag){     
                    break;     
                }     
            }     
            //删除子目录     
            else{     
                flag = deleteDirectory(files[i].getAbsolutePath());     
                if(!flag){     
                    break;     
                }     
            }     
        }     
             
        if(!flag){     
            System.out.println("删除目录失败");     
            return false;     
        }     
             
        //删除当前目录     
        if(dirFile.delete()){     
            System.out.println("删除目录"+dir+"成功！");     
            return true;     
        }else{     
            System.out.println("删除目录"+dir+"失败！");     
            return false;     
        }     
    } 


}
